package com.tianyiyan.dao.tyenword;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.tianyiyan.hibernate.tyenword.PhraseMeaning;
import com.tianyiyan.hibernate.tyenword.Book;
import com.tianyiyan.hibernate.tyenword.Phrase;
import com.tianyiyan.hibernate.tyenword.Word;
import com.tianyiyan.hibernate.tyenword.WordMeaning;
import com.tianyiyan.tyenword.Elem;
import com.tianyiyan.utils.HibernateUtils;

public class VocabularyDao
{
	/**
	 * @brief get Vocabulary List.
	 * For English To Chinese
	 * @param book
	 * @param unit
	 * @return Elem List.
	 */
	public List<Elem> getVocabularyList(Book book, int unit)
	{
		List<Elem> vocabularyList = new ArrayList<Elem>();
		Session session = HibernateUtils.getInstance().getSession();
		Transaction transaction = session.beginTransaction();
		try
		{
			Query queryPhrase = session.getNamedQuery("phraseFromBookUnit");
			Query queryWord = session.getNamedQuery("wordFromBookUnit");
			
			List<Phrase> phraseList = queryPhrase.setEntity("book", book).setInteger("unit", unit).list();
			List<Word> wordList = queryWord.setEntity("book", book).setInteger("unit", unit).list();
			
			for (Word w : wordList)
				vocabularyList.add(new Elem(w));
			for (Phrase p : phraseList)
				vocabularyList.add(new Elem(p));
			
			transaction.commit();
		}
		catch(Exception ex)
		{
			if (null != transaction)
			{
				transaction.rollback();
				ex.printStackTrace();
			}
		}
        return vocabularyList;
	}
	/**
	 * @brief get Meaning List.
	 * For Chinese To English
	 * @param book
	 * @param unit
	 * @return Elem List.
	 */
	public List<Elem> getMeaningList(Book book, int unit)
	{
		List<Elem> meaningList = new ArrayList<Elem>();
		Session session = HibernateUtils.getInstance().getSession();
		Transaction transaction = session.beginTransaction();
        try
        {
        	/// Step.1 Get All Phrase And Word
        	Query queryPhrase = session.getNamedQuery("phraseFromBookUnit");
        	Query queryWord = session.getNamedQuery("wordFromBookUnit");
        	
        	List<Phrase> phraseList = queryPhrase.setEntity("book", book).setInteger("unit", unit).list();
        	List<Word> wordList = queryWord.setEntity("book", book).setInteger("unit", unit).list();
        	
        	/// Step.2 Get Meaning String From Phrases And Words By Step.1
        	
        	for (Word w : wordList)
        		meaningList.add(new Elem(w.getId(), w.getMeaningCnStr().trim().replace("\n", "<br />"), Elem.ELEM_WORD_MEANING));
        	for (Phrase p : phraseList)
        		meaningList.add(new Elem(p.getId(), p.getMeaningCnStr().trim().replace("\n", "<br />"), Elem.ELEM_PHRASE_MEANING));
        	
        	transaction.commit();
        }
        catch(Exception ex)
        {
        	if (null != transaction)
			{
				transaction.rollback();
				ex.printStackTrace();
			}
        }
        return meaningList;
	}
	
	public List<Book> getBookList()
	{
		List<Book> bookList = new ArrayList<Book>();
		Session session = HibernateUtils.getInstance().getSession();
		Transaction transaction = session.beginTransaction();
		try
		{
			String hql = "FROM Book";
			Query query = session.createQuery(hql);
			
			bookList = query.list();
			
			transaction.commit();
		}
		catch(Exception ex)
		{
			if (null != transaction)
			{
				transaction.rollback();
				ex.printStackTrace();
			}
		}
		return bookList;
	}
	
	public List<Elem> filterError(List<Elem> answers)
	{
		List<Elem> results = new ArrayList<Elem>();
		Session session = HibernateUtils.getInstance().getSession();
		Transaction transaction = session.beginTransaction();
		try
		{
			for (Elem e : answers)
			{
				String elemType = e.getElemType();
				if (elemType.equals(Elem.ELEM_PHRASE_MEANING))
				{
					Phrase p = (Phrase)session.get(Phrase.class, e.getId());
					if (!(p.getVal().equals(e.getVal())))
						results.add(new Elem(e.getId(), p.getVal(), e.getElemType()));
				}
				else if (elemType.equals(Elem.ELEM_WORD_MEANING))
				{
					Word w = (Word)session.get(Word.class, e.getId());
					if (!(w.getVal().equals(e.getVal())))
						results.add(new Elem(e.getId(), w.getVal(), e.getElemType()));
				}
			}
			
			transaction.commit();
		}
		catch(Exception ex)
		{
			if (null != transaction)
			{
				transaction.rollback();
				ex.printStackTrace();
			}
		}
		return results;
	}
}
